package org.e_gold.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class StringUtil {

	private static final Logger logger = Logger.getLogger(StringUtil.class);

	public static String prepareCookies(Map<String, String> cookies, Map<String, String> currentCookies) {

		String strCookies = "";
		// first time
		if (currentCookies == null) {
			if (cookies != null) {
				for (Map.Entry<String, String> entry : cookies.entrySet()) {
					strCookies += ";" + entry.getKey() + "=" + entry.getValue();
				}
			}
		} else {
			// append cookies
			if (cookies != null) {
				for (Map.Entry<String, String> entry : cookies.entrySet()) {
					currentCookies.put(entry.getKey(), entry.getValue());
				}
				for (Map.Entry<String, String> entry : currentCookies.entrySet()) {
					strCookies += ";" + entry.getKey() + "=" + entry.getValue();
				}
			}
		}
		return strCookies.length() > 0 ? strCookies.substring(1) : "";
	}

	public static Float getBalanceFromString(String totalBalanceStr) {

		Float amount = 0F;
		if (!StringUtils.isEmpty(totalBalanceStr)) {
			totalBalanceStr = totalBalanceStr.substring(0, totalBalanceStr.length() - 4);
			amount = Float.parseFloat(totalBalanceStr);
		}
		return amount;
	}

	public static String getName(String text) {

		String[] _name = text.split(" ");
		String name = "";
		for (int j = 0; j < _name.length - 1; j++) {
			name += " " + _name[j];
		}
		return name.substring(1);
	}

	public static String getContractType(String contractStr) {

		if (!StringUtils.isEmpty(contractStr)) {
			String[] contractType = contractStr.split(" ");
			String typeName = "";
			for (int i = 1; i < contractType.length; i++) {
				typeName += " " + contractType[i];
			}
			return typeName.substring(1);
		}
		return null;
	}

	public static void main(String a[]) {

		int ab = Math.round(Float.parseFloat("7.5"));
		logger.info(ab);
	}

	public static String getDate() {

		return new SimpleDateFormat("YYYY-MM-dd_hh-mm-ss").format(new Date());
	}

	// ex : singaporetech.edu.sg.hiringboss.com -> singaporetech.edu.sg
	public static String getDir(String name) {

		String dir = "";
		String[] s_ = name.split("\\.");
		for (int i = 0; i < s_.length - 2; i++) {
			dir += "." + s_[i];
		}
		if (name.indexOf("lg") > -1) {
			return "lg";
		}
		return dir.substring(1);
	}

	public static double similarity(String s1, String s2) {
		if (s1.length() < s2.length()) { // s1 should always be bigger
			String swap = s1; s1 = s2; s2 = swap;
		}

		int bigLen = s1.length();
		if (bigLen == 0) { return 1.0; /* both strings are zero length */ }

		int distanceBetweenS2AndS2 = StringUtils.getLevenshteinDistance(s1, s2);
		return (bigLen - distanceBetweenS2AndS2) / (double) bigLen;
	}

	public static String buildUrl(String base, String href){
		String[] parts = base.split("/");
		String fullUrl = base;
		//prepare URL
		if(href.startsWith("../")){
			parts = (String[]) ArrayUtils.subarray(parts, 0, parts.length-1);
			parts[parts.length-1] = href.substring(3);
		}else{
			parts[parts.length-1] = href;
		}
		fullUrl = org.apache.commons.lang.StringUtils.join(parts, "/");
		return fullUrl;
	}
}
